import * as echarts from '../../ec-canvas/echarts';
var comm=require("../../utils/data.js")
// var datalist = comm.getStatic().out;
// var databas = comm.getStatic().money;
var serverData={};

// var datalist = ['直接访问1', '邮件营销', '联盟广告', '视频广告', '搜索引擎'];
// var databas = [
//   {value: 335, name: '直接访问1'},
//   {value: 310, name: '邮件营销'},
//   {value: 234, name: '联盟广告'},
//   {value: 135, name: '视频广告'},
//   {value: 1548, name: '搜索引擎'}
// ]

// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    ec: {
      // onInit: initChart,
      lazyLoad:false //新加
    }
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    var that=this;
    this.echarCanve = this.selectComponent("#mychart-dom-bar");
    comm.getServerData().then(function(res){    
      serverData=res;
     console.log(serverData);
      that.init();
    });
  },
  //初始化
  init:function(){
    this.echarCanve.init((canvas, width, height)=> {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      chart.setOption(this.getOption());
      return chart;
    })    
  },
  getOption:function(){
    var option = {
      legend: {
          orient: 'vertical',
          left: 'left',
          data: [serverData.data.classname[0].cate_name, serverData.data.classname[1].cate_name, serverData.data.classname[2].cate_name, serverData.data.classname[3].cate_name, serverData.data.classname[4].cate_name]
      },
      series: [
          {
              name: '访问来源',
              type: 'pie',
              radius: '55%',
              center: ['50%', '45%'],
              data: [
                  {value: serverData.data.data[0][0].number, name: serverData.data.classname[0].cate_name},
                  {value: serverData.data.data[0][1].number, name: serverData.data.classname[1].cate_name},
                  {value: serverData.data.data[0][2].number, name: serverData.data.classname[2].cate_name},
                  {value: serverData.data.data[0][3].number, name: serverData.data.classname[3].cate_name},
                  {value: serverData.data.data[0][4].number, name: serverData.data.classname[4].cate_name}
                ],
          }
      ]}
      return option;
    },




  getUserInfo(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
